package com.itboye.sunsun.person.ui;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.sunsun.HomeFragment;
import com.itboye.sunsun.MainActivity;
import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.MyCollectAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.MyCollectBean;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.mycollect.fragments.CollectGoodsFragment;
import com.itboye.sunsun.mycollect.fragments.CollectWordsFragment;
import com.itboye.sunsun.support.BaseActivity;

public class MyCollectActivity extends BaseActivity implements OnClickListener{



	private Fragment[] fragments = new Fragment[2];

	private int index;
	private int currentIndex;

	private View leftrela;
	private View rightrela;
	private TextView[] texts;

	private XListView xlistview;
	private ImageView stateimageout;
	private List<MyCollectBean> list;
	private RelativeLayout midrela;
	private MyCollectAdapter mAdapter;
	private ImageView imagestatein;
	private RelativeLayout backrela;
	private Button delete;
	boolean state = false;

	@SuppressLint("ResourceAsColor")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mycollect);
		setStatusBarColor(R.color.top_blue);
		initView();

		List<Fragment> list = getSupportFragmentManager().getFragments();
		if (list != null) {
			for (int i = 0; i < fragments.length; i++) {
				fragments[i] = getSupportFragmentManager().findFragmentByTag(
						"" + i);
			}
			index = savedInstanceState.getInt("tab");
			switch (index) {
			case 0:
				onClick(leftrela);
				break;

			case 1:
				onClick(rightrela);
				break;
			}
		} else {

			fragments[0] = new CollectGoodsFragment(MyCollectActivity.this);

			getSupportFragmentManager().beginTransaction()
					.add(R.id.fragment_container, fragments[0], "0")
					.show(fragments[0]).commit();
		}

	}

	public void initView() {

		leftrela.setOnClickListener(this);
		rightrela.setOnClickListener(this);
		texts = new TextView[2];
		texts[0] = (TextView) findViewById(R.id.goodstext);
		texts[1] = (TextView) findViewById(R.id.wordstext);
		texts[0].setSelected(true);

	}

	// public void initData() {
	//
	// list = new ArrayList<MyCollectBean>();
	// MyCollectBean bean = new MyCollectBean();
	// bean.setType("hah");
	// MyCollectBean bean1 = new MyCollectBean();
	// bean1.setType("a");
	// MyCollectBean bean2 = new MyCollectBean();
	// bean2.setType("1");
	// list.add(bean);
	// list.add(bean1);
	// list.add(bean2);
	// mAdapter = new MyCollectAdapter(App.ctx, list);
	// xlistview.setAdapter(mAdapter);
	//
	// }

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftrela:
			index = 0;
			if (fragments[0] == null) {
				fragments[0] = new CollectGoodsFragment(MyCollectActivity.this);
			}

			texts[0].setTextColor(getResources().getColor(R.color.orange));
			texts[1].setTextColor(getResources().getColor(R.color.home_gray));

			break;
		case R.id.rightrela:
			index = 1;
			if (fragments[1] == null) {
				fragments[1] = new CollectWordsFragment(MyCollectActivity.this);
			}

			texts[0].setTextColor(getResources().getColor(R.color.home_gray));
			texts[1].setTextColor(getResources().getColor(R.color.orange));
			break;
		case R.id.backrela:
			this.finish();
			break;
		case R.id.delete:
			if(index==0)
			{
			   DeleteGoodsListener listener= (DeleteGoodsListener) getSupportFragmentManager().findFragmentByTag("0");
		       listener.onDeleteCollectGoods();
			}else if(index==1)
			{
				DeleteWordsListener listener=(DeleteWordsListener) getSupportFragmentManager().findFragmentByTag("1");
				listener.onDeleteCollectWords();
			}
			break;
			default:
				break;
		}
		for (int j = 0; j < fragments.length; j++) {
			if (fragments[j]!=null) {
				getSupportFragmentManager().beginTransaction().hide(fragments[j]).commitAllowingStateLoss();
			}
		}
		
		if (!fragments[index].isAdded()) {
			getSupportFragmentManager().beginTransaction()
			.add(R.id.fragment_container, fragments[index],""+index).commitAllowingStateLoss();
		}
		
		getSupportFragmentManager().beginTransaction().show(fragments[index]).commitAllowingStateLoss();

			// case R.id.midrela:
			// case R.id.stateimageout:
			// if (state == false) {
			// stateimageout.setBackground(getResources().getDrawable(
			// R.drawable.select));
			// state = true;
			// } else {
			// stateimageout.setBackground(getResources().getDrawable(
			// R.drawable.un_select));
			// state = false;
			// }
			//
			// for (int i = 0; i < list.size(); i++) {
			// if (!state)
			//
			// {
			// list.get(i).setSelected(false);
			//
			// } else {
			// list.get(i).setSelected(true);
			// }
			// }
			// mAdapter.notifyDataSetChanged();
			// break;
		

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putInt("tab", index);
	}
	
	
	
	//删除收藏的商品
	public interface DeleteGoodsListener
	{
		public void onDeleteCollectGoods();
	}
	//删除收藏的帖子
	public interface DeleteWordsListener
	{
		public void onDeleteCollectWords();
	}
//	@Override
//	public void onDeleteCollectGoods() {
//		// TODO Auto-generated method stub
//		
//	}

	// @SuppressLint("NewApi")
	// @Override
	// public void onItemClick(AdapterView<?> arg0, View view, int position,
	// long arg3) {
	//
	// MyCollectBean myCollectBean = list.get(position - 1);
	// myCollectBean.setSelected(!myCollectBean.isSelected());
	//
	// mAdapter.notifyDataSetChanged();
	//
	// }

}
